package games.padinvaders;

import utils.RandomNumber;

import graphics.Sprite2D;
import hardware.Display2D;

public class InvaderProjectile extends Sprite2D{

public static final short [] projectileColor = {0x5f,0x66,0x303,0x404};
	
public static final int PROJECTILE_TYPE_LASER		= 0;	
public static final int PROJECTILE_TYPE_SHELL		= PROJECTILE_TYPE_LASER+1;
public static final int PROJECTILE_TYPE_BOMB		= PROJECTILE_TYPE_SHELL+1;
public static final int PROJECTILE_TYPE_SUPER_BOMB	= PROJECTILE_TYPE_BOMB+1;



public static final int DAMAGE_LASER				= 5;	
public static final int DAMAGE_SHELL				= 10;


private int type;



public void draw()
{
	//int color;
	
	if (!visible)
		return;
	
	
	if ((type==PROJECTILE_TYPE_BOMB) && (yPos==0))
		{
		explode();
		}
	else
		Display2D.setPixel((byte)xPos,(byte) yPos, color);
		
}
	


public void fire(int type,int xPos,int yPos)
{
	this.type= type;
	this.xPos=xPos;
	this.yPos=yPos;
	visible=true;
	color=projectileColor[type];
	
	animationCounter=0xff;
}



public void move()
{
	boolean doMove=false;
	

	
	if (!visible)
		return;

	animationCounter++;
	
	switch(type)
	{
		case PROJECTILE_TYPE_LASER:
		if (animationCounter>3)
			doMove=true;
			break;

		case PROJECTILE_TYPE_SHELL:
			if (animationCounter>5)
				doMove=true;			
			break;
			
		case PROJECTILE_TYPE_BOMB:
			if (animationCounter>10)
				doMove=true;
			break;

		case PROJECTILE_TYPE_SUPER_BOMB:
			if (animationCounter>15)
				doMove=true;
			break;			
			
		}
			
		if (doMove==true)
			{
			animationCounter=0;
			yPos--;

			if (yPos<0) 
				visible=false;

			}
	
	
}





public void explode()
{

	short color;
	
	switch (animationCounter)
	{
	case 0:
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
	case 6:
		color=(short) (0x03+(0x22*animationCounter));
		Display2D.setPixel((byte)xPos,(byte) yPos, color);
		break;

	case 7:
	case 8:
	case 9:
	case 10:
		color=(short) (0x03+(0x22*animationCounter));
		Display2D.setPixel((byte)xPos,(byte) yPos, color);

		color=(short) (0x03+(0x22*(animationCounter-7)));
		Display2D.setPixel((byte)xPos-1,(byte) yPos, color);
		Display2D.setPixel((byte)xPos+1,(byte) yPos, color);
	
		break;


		
	default:
			yPos--;
			break;
		
	}
	
	animationCounter++;		
	
	
}




public int doDamage(int victimXPos,int victimYPos)
{
	int damage;
	

	
	if (!visible)
		return(0);
	
	
	damage=0;
	

		
	switch (type)
	{
	case PROJECTILE_TYPE_LASER:
		 if ((xPos==victimXPos) && (yPos==victimYPos))
		 	{
			 damage=DAMAGE_LASER;
			 visible=false;
		 	}
		break;
		
		
	case PROJECTILE_TYPE_SHELL:
		 if ((xPos==victimXPos) && (yPos==victimYPos))
		 	{
			 damage=DAMAGE_SHELL;
			 visible=false;
		 	}
		break;
		
		
		
		

	case PROJECTILE_TYPE_BOMB:
		 if ((xPos==victimXPos) && (yPos==victimYPos) )
		 	{
			 damage=10;
		 	}
		 else if ((victimXPos>=(xPos-1)) && (victimXPos<=(xPos+1)) &&
				 (victimYPos>=(yPos-1)) && (victimYPos<=(yPos+1)) )
			 	{
				 damage=1;
			 	}		 
		break;
		
	}
	

	
	return (damage);
}


}
